fw_start_root(){
# Usuário do sistema que recebera permissão na interface virtual
system_user=rodrigo

# Adicionado interfaces
tunctl -u $system_user
tunctl -u $system_user
tunctl -u $system_user

# Levantando as interfaces
ifconfig tap0 192.168.20.1 netmask 255.255.255.0
ifconfig tap1 192.168.30.1 netmask 255.255.255.0
ifconfig tap2 192.168.40.1 netmask 255.255.255.0

# Ativando o roteamento de pacotes
echo 1 > /proc/sys/net/ipv4/ip_forward
# Mascarando interface
#iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
}

fw_start(){
echo "Subindo uml_switch"
echo "/tmp/net100.ctl"
./uml_switch -unix /tmp/net100.ctl &
echo "/tmp/net200.ctl"
./uml_switch -unix /tmp/net200.ctl &
echo "/tmp/net101.ctl"
./uml_switch -unix /tmp/net101.ctl &
echo "/tmp/net201.ctl"
./uml_switch -unix /tmp/net201.ctl &
echo "/tmp/net300.ctl"
./uml_switch -unix /tmp/net300.ctl &
echo "/tmp/net301.ctl"
./uml_switch -unix /tmp/net301.ctl &

echo "Subindo maquinas virtuais"
xterm -title HOST1 -e ./linux_fev_2009 ubda=./cow-host1,./Debian-4.0-fev_2009 eth0=daemon,,,/tmp/net100.ctl eth1=daemon,,,/tmp/net101.ctl eth2=daemon,,,/tmp/net300.ctl &
xterm -title HOST2 -e ./linux_fev_2009 ubda=./cow-host2,./Debian-4.0-fev_2009 eth0=daemon,,,/tmp/net201.ctl eth1=daemon,,,/tmp/net200.ctl eth2=daemon,,,/tmp/net301.ctl &
xterm -title ROT1 -e ./linux_fev_2009 ubda=./cow-host-rot1,./Debian-4.0-fev_2009 eth0=daemon,,,/tmp/net100.ctl eth1=daemon,,,/tmp/net200.ctl &
xterm -title ROT2 -e ./linux_fev_2009 ubda=./cow-host-rot2,./Debian-4.0-fev_2009 eth0=daemon,,,/tmp/net201.ctl eth1=daemon,,,/tmp/net101.ctl &
xterm -title QNI -e ./linux_fev_2009 ubda=./cow-host-qni,./Debian-4.0-fev_2009 eth0=daemon,,,/tmp/net300.ctl &
xterm -title QNR -e ./linux_fev_2009 ubda=./cow-host-qnr,./Debian-4.0-fev_2009 eth0=daemon,,,/tmp/net301.ctl &


#xterm -geometry 60x20 -T Itália -e "./linux_rot umid=italia  ubda=./cow-italia,./n3 mem=32M eth0=daemon,,,/tmp/net100.ctl eth1=daemon,,,/tmp/net101.ctl eth2=daemon,,,/tmp/net102.ctl" &

#xterm -geometry 60x20 -T Brasil -e "./linux_fev_2009 umid=brasil  ubda=./cow-brasil,./n3 mem=32M eth0=daemon,,,/tmp/net200.ctl eth1=daemon,,,/tmp/net201.ctl eth2=daemon,,,/tmp/net202.ctl" &


# Subindo antigas VM
#xterm -title IP-20.2 -e ./linux-ro ubd0=./cow-host0 eth0=tuntap,tap0 &
#xterm -title IP-30.2 -e ./linux-ro ubd0=./cow-host1 eth0=tuntap,tap1 &
#xterm -title IP-40.2 -e ./linux-ro ubd0=./cow-host2 eth0=tuntap,tap2
}


fw_start_new(){
echo "Subindo novas maquinas virtuais"
# Subindo Novas VM
xterm -title IP-20.2 -e ./linux-ro ubd0=./cow-host0,./Debian-4.0-x86-root_fs eth0=tuntap,tap0 &
xterm -title IP-30.2 -e ./linux-ro ubd0=./cow-host1,./Debian-4.0-x86-root_fs eth0=tuntap,tap1 &
xterm -title IP-40.2 -e ./linux-ro ubd0=./cow-host2,./Debian-4.0-x86-root_fs eth0=tuntap,tap2
}


fw_stop(){
killall uml_switch linux_fev_2009
rm /tmp/net*.ctl
}

fw_usage(){
  echo
  echo "$0 (start | starttun | stop | restart )"
  echo
  echo "start   - Inicia as máquinas virtuais"
  echo "startun - Cria 3 interfaces tun"
  echo "stop    - Derruba as máquinas virtuais e uml_switch"
  echo "restart - Para e inicia máquinas virtuais"
}

# Case...

case $1 in

  start)
     fw_start;
  ;;

  startnew)
     fw_start_new;
  ;;

  startun)
        # Se root...
        if [ $(whoami) = 'root' ]; then
             fw_start;
        else

        echo "          *****************"
        echo "          * Are you root? *"
        echo "          *****************"
        fi
  ;;

  stop)
     fw_stop;
  ;;
 restart)
    fw_stop;
    fw_start;
  ;;

  *)
     fw_usage;
     exit;
  ;;
esac
